﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL.Util;
using DTO;
using BLL;

namespace MZ.Money
{
    public partial class CardManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DdlCardType.DataSource = EnumHelper.GetEnumEnumerator(typeof(CardType));
                DdlCardType.DataBind();
                BindCard();
                BindBank();
            }

        }
        protected void BtnCreadCard_Click(object sender, EventArgs e)
        {
            var cardType = EnumHelper.GetEnumByValue<CardType>(typeof(CardType), DdlCardType.SelectedValue);
            Card card = new Card();
            Account account = CacheHelper.GetAccount(User.Identity.Name);
            card.AID = account.ID;
            card.Title = TxtCardName.Text;
            card.Type = cardType;
            card.Description = TxtDescription.Text;
            switch (cardType)
            {
                case CardType.Cash:
                    AddCashCard(card);
                    break;
                case CardType.DebitCard:
                    AddDebitCard(card);
                    break;
                case CardType.CreditCard:
                    AddCreditCard(card);
                    break;
                case CardType.StockCard:
                    AddStockCard(card);
                    break;
                default:
                    AddCashCard(card);
                    break;
            }
            
            CardBLL.AddCard(card);
            BindCard();
        }

        protected void BtnEditCard_Click(object sender, EventArgs e)
        {
            var cardType = EnumHelper.GetEnumByValue<CardType>(typeof(CardType), DdlCardType.SelectedValue);
            Card card = new Card();
            card.CID = int.Parse(HfieldCID.Value);
            Account account = CacheHelper.GetAccount(User.Identity.Name);
            card.AID = account.ID;
            card.Title = TxtCardName.Text;
            card.Type = cardType;
            card.Description = TxtDescription.Text;
            switch (cardType)
            {
                case CardType.Cash:
                    AddCashCard(card);
                    break;
                case CardType.DebitCard:
                    AddDebitCard(card);
                    break;
                case CardType.CreditCard:
                    AddCreditCard(card);
                    break;
                case CardType.StockCard:
                    AddStockCard(card);
                    break;
                default:
                    AddCashCard(card);
                    break;
            }

            CardBLL.EditCard(card);
            BindCard();
        }

        #region add card
        /// <summary>
        /// 添加现金账号
        /// </summary>
        private void AddCashCard(Card card)
        {
            card.Money = decimal.Parse(TxtCardMoney.Text);
        }
        private void AddDebitCard(Card card)
        {
            card.Money = decimal.Parse(TxtCardMoney.Text);
            card.CardNumber = TxtCardNo.Text;
            card.OrganizationID = int.Parse(DdlBank.SelectedValue);
        }
        /// <summary>
        /// 添加信用卡
        /// </summary>
        /// <param name="card"></param>
        private void AddCreditCard(Card card)
        {
            card.Money = 0;
            card.CreditLine = decimal.Parse(TxtCredit.Text);
            card.OrganizationID = int.Parse(DdlBank.SelectedValue);
            card.DueDate = DateTime.Parse(TxtDueDate.Text);
            card.CardNumber = TxtCardNo.Text;
            card.BillDate = DateTime.Parse(TxtBillDate.Text);
        }
        private void AddStockCard(Card card)
        {
            card.Money = decimal.Parse(TxtCardMoney.Text);
        }
        #endregion
        
        /// <summary>
        /// 绑定消费账户
        /// </summary>
        private void BindCard()
        {
            CardGrid.DataSource = CacheHelper.GetMyCard(User.Identity.Name);
            CardGrid.DataBind();
        }
        /// <summary>
        /// 绑定银行信息
        /// </summary>
        private void BindBank()
        {
            DdlBank.DataSource = OrganizationBLL.GetAllBank();
            DdlBank.DataBind();
        }

        protected string GetOrganizationTag(object id)
        {
            string html = "<a href='{0}' class='a_new'  target='_blank'>{1}<img src='../img/open_new_window.png' title='新窗口打开' alt='新窗口打开' /></a>";
            var org = OrganizationBLL.GetOrganizationById(int.Parse(id.ToString()));
            if (null==org)
            {
                return string.Empty;
            }
            else
            {
                return string.Format(html, org.Url, org.Title);
            }
        }
    }
}
